package com.sdp.wx.controller;


import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.SnowFlake;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_repair_vehicle_doc.entity.WxRepairVehicleDoc;
import com.sdp.wx.db.wx_repair_vehicle_doc.service.IWxRepairVehicleDocService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-03-04
 */
@RestController
@RequestMapping("/WxRepairVehicleDocController")
public class WxRepairVehicleDocController {

	@Autowired
	private Query query;
	
	@Autowired
	private IWxRepairVehicleDocService iWxRepairVehicleDocService;
	
	@Value("${sdp.filePath}")
    private String filePath;
	
	@Autowired
	private SnowFlake snowFlake;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	/**
	 * 所有系统上传的文件都存在此文件夹下
	 */
	private String sdpFiles = "sdpFiles";
	
	private String customFilePath = "repairvehicledoc";

	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxRepairVehicleDocService, WxRepairVehicleDoc.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxRepairVehicleDoc repairVehicleDoc) throws IOException {
		//文件类型
		String fileType = "docx";
		String filekey = snowFlake.nextId()+"";
		//创建的文件名
		String fileName = filekey+"."+fileType;
		if(repairVehicleDoc==null) {
			return Result.error("信息为空");
		}
		if(StringUtils.isEmpty(repairVehicleDoc.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxRepairVehicleDocService, WxRepairVehicleDoc.class, new String[] {"docName"}, "keyId", repairVehicleDoc, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("单据名称不能重复");
			}
			String filePath = this.filePath + File.separator+this.sdpFiles+File.separator+customFilePath;
			String dbpath = sdpFiles+File.separator+customFilePath + File.separator + fileName;
			File target = new File(filePath);
			//创建目录
			if (!target.exists()) {
				target.mkdirs();
			}
			//创建文件
			File file = new File(filePath+File.separator+fileName);
            file.createNewFile();
            repairVehicleDoc.setFileKey(filekey);
            repairVehicleDoc.setFilePath(dbpath);
			repairVehicleDoc.setCreateTime(LocalDateTime.now());
			iWxRepairVehicleDocService.save(repairVehicleDoc);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxRepairVehicleDocService, WxRepairVehicleDoc.class, new String[] {"docName"}, "keyId", null, repairVehicleDoc, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("单据名称不能重复");
			}
			iWxRepairVehicleDocService.updateById(repairVehicleDoc);
		}
		return Result.ok(repairVehicleDoc);
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iWxRepairVehicleDocService.removeById(keyId);
		return Result.ok();
	}
}

